package com.surface.hao.frame;

import com.surface.hao.component.ScrollPanel;
import com.surface.hao.util.ComponentUtils;
import com.surface.hao.util.DateUtils;
import com.surface.hao.util.LogUtils;
import jdk.nashorn.internal.objects.NativeObject;
import lombok.extern.log4j.Log4j;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Description 日期与时间戳的互相转换
 * @Date 2019/12/29
 * @Author songhao
 **/
@Log4j
public class DateFormatPanel{

    static ScrollPanel tipArea = null;

    public static JPanel getJpanel(String titleName) {
        JPanel panel = new JPanel();
        //设置布局方式和大小
        panel.setLayout(new BorderLayout(0, 0));
        panel.setSize(800, 600);

        JLabel titleLabel = new JLabel(titleName,JLabel.CENTER);
        titleLabel.setBorder(new EmptyBorder(10, 10, 25, 10));
        titleLabel.setFont(new Font("宋体", Font.BOLD, 24));

        //第一个Panel
        JPanel titlePanel = new JPanel();
        titlePanel.setLayout(new BorderLayout(0, 0));
        panel.add(titlePanel, BorderLayout.NORTH);

        //添加标题信息
        titlePanel.add(titleLabel, BorderLayout.NORTH);

        // 时间戳转换为日期这个Panel
        JPanel timeToDatePanel = getTimePanel();
        titlePanel.add(timeToDatePanel, BorderLayout.CENTER);

        // 日期字符串转换为时间戳Panel
        JPanel dateToTimePanel = getDatePanel();
        titlePanel.add(dateToTimePanel, BorderLayout.SOUTH);

        //第二个Panel
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BorderLayout(0, 0));
        panel.add(centerPanel, BorderLayout.CENTER);

        tipArea = new ScrollPanel();
        tipArea.setPreferredSize(new Dimension(200, 350));
        centerPanel.add(tipArea, BorderLayout.CENTER);
        return panel;
    }

    /**
     * 获取时间戳转换为日期这个Panel
     * @return
     */
    private static JPanel getTimePanel() {
        JPanel timePanel = new JPanel();
        FlowLayout flowLayout_1 = (FlowLayout) timePanel.getLayout();
        flowLayout_1.setAlignment(FlowLayout.LEFT);
        timePanel.setBorder(new TitledBorder(UIManager
                .getBorder("TitledBorder.border"), "日期转时间戳",
                TitledBorder.LEADING, TitledBorder.TOP, null,
                new Color(0, 0, 0)));

        //第一个时间戳输入框
        JTextField timeText = new JTextField();
        timeText.setText("");
        timeText.setFont(new Font("宋体", Font.BOLD, 24));
        timeText.setPreferredSize(new Dimension(200, 40));
        timePanel.add(timeText);

        JButton tranButton = new JButton("转换>>");
        tranButton.setPreferredSize(new Dimension(100, 40));
        timePanel.add(tranButton);

        JTextField dateText = new JTextField();
        dateText.setText("");
        dateText.setFont(new Font("宋体", Font.BOLD, 24));
        dateText.setPreferredSize(new Dimension(300, 40));
        timePanel.add(dateText);

        //获取一个下拉框组件
        JPanel areaPanel = new JPanel();
        JComboBox cmb = ComponentUtils.getJcomboBox();   //创建JComboBox
        areaPanel.add(cmb);
        timePanel.add(areaPanel);

        //设置button监控
        tranButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try{
                    //获取当前下拉框的值
                    log.info("a.getSelectedItem() = " + cmb.getSelectedItem() + "---" + cmb.getSelectedIndex());
                    //获取对应的内容
                    Long timeValue = Long.parseLong(timeText.getText().trim());
                    ComponentUtils.setTips(tipArea,"时间戳:[" + timeValue + "]当前选择的单位是:" + cmb.getSelectedItem());

                    //如果是秒的话，那么乘以1000
                    if(cmb.getSelectedIndex() == 0){
                        timeValue = timeValue * 1000;
                    }
                    //将内容设置到对应的输入框
                    dateText.setText(DateUtils.getValueByTime(timeValue,DateUtils.YMD_HMS_FORMAT));
                }catch (Exception ex){
                    log.error(ex.getMessage());
                    ComponentUtils.setTips(tipArea, LogUtils.toStackTrace(ex));
                }
            }
        });
        return timePanel;
    }

    /**
     * 获取日期转换为时间戳的Panel
     * @return
     */
    public static JPanel getDatePanel(){
        JPanel datePanel = new JPanel();
        FlowLayout flowLayout = (FlowLayout) datePanel.getLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);
        datePanel.setBorder(new TitledBorder(UIManager
                .getBorder("TitledBorder.border"), "时间戳转日期",
                TitledBorder.LEADING, TitledBorder.TOP, null,
                new Color(0, 0, 0)));

        JTextField dateText = new JTextField();
        dateText.setText("");
        dateText.setFont(new Font("宋体", Font.BOLD, 24));
        dateText.setPreferredSize(new Dimension(300, 40));
        datePanel.add(dateText);


        //获取一个下拉框组件
        JPanel areaPanel = new JPanel();
        JComboBox cmb = ComponentUtils.getJcomboBox();   //创建JComboBox
        areaPanel.add(cmb);
        datePanel.add(areaPanel);

        JButton tranButton = new JButton("转换>>");
        tranButton.setPreferredSize(new Dimension(100, 40));
        datePanel.add(tranButton);

        JTextField timeText = new JTextField();
        timeText.setText("");
        timeText.setFont(new Font("宋体", Font.BOLD, 24));
        timeText.setPreferredSize(new Dimension(200, 40));
        datePanel.add(timeText);

        tranButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try{
                    //获取当前下拉框的值
                    log.info("a.getSelectedItem() = " + cmb.getSelectedItem() + "---" + cmb.getSelectedIndex());
                    //获取对应的内容
                    String timeValue = dateText.getText();
                    Long timeStamp = DateUtils.getMillisecondsByValue(timeValue,DateUtils.YMD_HMS_FORMAT);
                    ComponentUtils.setTips(tipArea,"时间格式:[" + timeValue + "]当前选择的单位是:" + cmb.getSelectedItem());

                    //如果是秒的话，那么乘以1000
                    if(cmb.getSelectedIndex() == 0){
                        timeStamp = timeStamp / 1000;
                    }
                    //将内容设置到对应的输入框
                    //将内容设置到对应的输入框
                    timeText.setText(timeStamp + "");
                }catch (Exception ex){
                    log.error(ex.getMessage());
                    ComponentUtils.setTips(tipArea, LogUtils.toStackTrace(ex));
                }
            }
        });
        return datePanel;
    }

}
